package com.cenwangkeji.resident.ui.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.BasicInfoBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.TestingBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.base.BaseDialogFragment;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.utils.common.UIUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/10/28 17:13
 * 测量血压
 */
public class TestingDialog extends BaseDialogFragment {
    @BindView(R.id.et_blood_pressure)
    EditText mEtBloodPressure;//血压
    @BindView(R.id.et_weight)
    EditText mEtWeight;//体重
    @BindView(R.id.et_blood_glucose)
    EditText mEtBloodGlucose;//
    @BindView(R.id.et_heart_rate)
    EditText mEtHeartRate;//
    Unbinder unbinder;

    @Override
    public int setLayoutId() {
        return R.layout.dialog_testing;
    }

    @Override
    protected void initWindow(Window window) {
        setLayoutParams(window, Gravity.CENTER, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, 0, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);

        return rootView;
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                save();
                break;
        }
    }

    private void save(){
      final String pressure = mEtBloodPressure.getText().toString();
      final String weight = mEtWeight.getText().toString();
      final String heartRate = mEtHeartRate.getText().toString();
      final String glucose = mEtBloodGlucose.getText().toString();
      if (TextUtils.isEmpty(pressure)){
          MyToast.show("血压不能为空");
        }else  if (TextUtils.isEmpty(weight)){
          MyToast.show("体重不能为空");
        }else  if (TextUtils.isEmpty(heartRate)){
          MyToast.show("心率不能为空");
        }else  if (TextUtils.isEmpty(glucose)){
          MyToast.show("血糖不能为空");
      }else {
          Observable.create(new ObservableOnSubscribe<SuccessBean>() {
              @Override
              public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                  Map<Object,Object> map = new HashMap<>();
                  map.put(Constant.token, User.getToken());
                  map.put("mark",3);
                  map.put("gId",User.getUserId());
                  map.put("zrys",User.getDoctorId());
                  map.put("xy", pressure);
                  map.put("tz",weight);
                  map.put("xl", heartRate);
                  map.put("xt",glucose);
                  JSONObject jsonObject = new JSONObject(map);
                  MyLogger.i(jsonObject.toString());
                  String json = OkHttpUtils.post(jsonObject.toString(), Constant.add_tesing);
                  Gson gson = new Gson();
                  emitter.onNext(gson.fromJson(json, SuccessBean.class ));

              }
          }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
              @Override
              public void onNext(SuccessBean successBean) {
                  if (successBean.getCode()==0){
                      MyToast.show("添加成功");
                      mListener.onAdd();
                      dismiss();
                  }else {
                      MyToast.show(successBean.getMsg());
                  }

              }
          });
      }

    }

    private Listener mListener;

    public void setListener(Listener listener){
            mListener = listener;

    }
    public interface Listener{
        void onAdd();
    }
}
